package backend

import (
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/model/app"
	"chatgpt/services"
	"github.com/gin-gonic/gin"
)

// 权益列表
func BenefitListHandle(ctx *gin.Context) {
	list := services.BenefitSrv.GetList()
	helper.Count(ctx, code.SUCCESS, map[string]any{
		"Total": 0,
		"List":  list,
	})
}

// 权益保存
func BenefitSaveHandle(ctx *gin.Context) {
	a, _ := ctx.Get("RawAny")
	raw, ok := a.(map[string]any)
	if !ok {
		helper.Count(ctx, code.REQ_PARAM_ERR, nil)
		return
	}
	bene := app.Benefit{}
	bene.ConvFromMap(raw)
	mc := services.BenefitSrv.Save(&bene)
	helper.Count(ctx, mc, bene)
}

// 权益删除
func BenefitDeleteHandle(ctx *gin.Context) {

}

// 权限分配
func BenefitScopeHandle(ctx *gin.Context) {
	a, _ := ctx.Get("RawAny")
	raw, ok := a.(map[string]any)
	if !ok {
		helper.Count(ctx, code.REQ_PARAM_ERR, nil)
		return
	}
	bs := app.BenefitScope{}
	bs.ConvFromMap(raw)
	mc := services.BenefitSrv.BenefitScope.Save(&bs)
	helper.Count(ctx, mc, bs)
}

// 权限item 添加修改
func ScopeSaveHandle(ctx *gin.Context) {
	a, _ := ctx.Get("RawAny")
	raw, ok := a.(map[string]any)
	if !ok {
		helper.Count(ctx, code.REQ_PARAM_ERR, nil)
		return
	}
	scope := app.Scope{}
	scope.ConvFromMap(raw)
	mc := services.BenefitSrv.Scope.Save(&scope)
	helper.Count(ctx, mc, scope)
}

// 权限item list
func ScopeListHandle(ctx *gin.Context) {
	data_list := services.BenefitSrv.Scope.GetList(0, 0)
	total := services.BenefitSrv.Scope.Total(nil)
	helper.Count(ctx, code.SUCCESS, map[string]any{
		"Total": total,
		"List":  data_list,
	})
}

// 后台专属接口，强制为某个用户绑定权益
func BenefitForceHandle(ctx *gin.Context) {
	a, _ := ctx.Get("RawAny")
	raw, ok := a.(map[string]any)
	if !ok {
		helper.Count(ctx, code.REQ_PARAM_ERR, nil)
		return
	}
	scope := app.BenefitUser{}
	scope.ConvFromMap(raw)
	mc := services.BenefitSrv.BenefitUser.Save(&scope)
	helper.Count(ctx, mc, scope)
}
